package com.yuke.cloud.service.omc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.omc.dto.OdsPointsDto;
import com.yuke.cloud.service.omc.entity.OdsUserPoint;
import com.yuke.cloud.service.omc.service.OdsUserPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by zn on 2019/12/25.
*/
@Api(value = "WEB - OdsUserPointController", tags = "处理OdsUserPoint相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/ods/user_point")
public class OdsUserPointController extends BaseController {
    @Resource
    private OdsUserPointService odsUserPointService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "odsUserPoint", value = "该数据记录的POJO") @RequestBody OdsUserPoint odsUserPoint) {
        odsUserPointService.save(odsUserPoint);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        odsUserPointService.deleteById(id);
        return WrapMapper.ok();
    }

    @PostMapping("/update")
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "odsUserPoint", value = "需要修改的内容") @RequestBody OdsUserPoint odsUserPoint) {
        odsUserPointService.update(odsUserPoint);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<OdsUserPoint> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        OdsUserPoint odsUserPoint = odsUserPointService.findById(id);
        return WrapMapper.ok(odsUserPoint);
    }

    /**
     * 获取全部用户积分
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/getUserPoints")
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<OdsUserPoint>> allList(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size
                                               ) {
        PageHelper.startPage(page, size);
        List<OdsUserPoint> list = odsUserPointService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 获取用户个人积分
     * @param page
     * @param size
     * @param odsUserPoint
     * @return
     */
    @PostMapping("/getUserPoint")
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                @RequestBody(required = false) OdsUserPoint odsUserPoint) {

        OdsPointsDto userPoints = odsUserPointService.getUserPoints(odsUserPoint);
        return WrapMapper.ok(userPoints);
    }
}
